<?php
class Server {
  public static function autocomplete0($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('term', '');
    
    $opts = new stdClass();
    $opts->cd = 't.track_id LIKE :v1';
    $opts->cdv = array(':v1' => '%' . $req . '%');
    $rslt = $orm->get('pb_colg_invoice', 't.track_id', 'ASC', 1, SIZE, $opts)->d;
    $res = array();
    foreach ($rslt as $itm) {
      $obj = new stdClass();
      $obj->label = $itm->track_id;
      $obj->value = $itm->track_id;
      $res[] = $obj;
    }
    echo json_encode($res);
  }
  
  public static function autocomplete2($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('term', '');
    
    $opts = new stdClass();
    $opts->cd = 't.title__ LIKE :v1';
    $opts->cdv = array(':v1' => '%' . $req . '%');
    $rslt = $orm->get('pb_colg_student', 't.title__', 'ASC', 1, SIZE, $opts)->d;
    $res = array();
    foreach ($rslt as $itm) {
      $obj = new stdClass();
      $obj->label = $itm->title__;
      $obj->value = $itm->title__;
      $obj->id = $itm->id;
      $obj->user_id = $itm->user_id;
      $obj->user_value = $itm->user_title__;
      $res[] = $obj;
    }
    echo json_encode($res);
  }
  
  public static function search($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.track_id LIKE :v1 AND t.student_title__ LIKE :v2';
    $opts->cdv = array(':v1' => '%' . $req->term0 . '%', ':v2' => '%' . $req->term2 . '%');
    $res = $orm->get('pb_colg_invoice', $req->o, $req->q, $req->pg, SIZE, $opts);
    echo json_encode($res);
  }
  
  public static function submit_inv($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    if ($req->student_id == 0) {
      echo 'Student is empty';
      return;
    }
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_colg_invoice', $req);
    } else {
      $orm->add('pb_colg_invoice', $req);
      $req->id = $orm->last_id();
    }
    
    if ($req->__paid != 0 || $req->__discount != 0) {
      $req2 = clone $req;
      $req2->date = $req2->__date;
      $req2->invoice_id = $req2->id;
      $req2->invoice_track_id = $req2->track_id;
      $req2->amount = $req2->__paid;
      $req2->discount = $req2->__discount;
      unset($req2->balance__);
      unset($req2->track_id);
      unset($req2->due_date);
      unset($req2->received__);
      $orm->add('pb_colg_payment', $req2);
    }

    if ($req->enrollment_id != 0) {
      ColgUtils::bal_enrl($orm, $req->enrollment_id);
    }
    ColgUtils::bal_student($orm, $req->student_id);
    
    unset($req->__paid);
    unset($req->__discount);
    unset($req->__date);
    echo json_encode($req);
  }
  
  public static function get_payments($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('req', 0);
    
    $opts = new stdClass();
    $opts->cd = 't.invoice_id = :v1';
    $opts->cdv = array(':v1' => $req);
    $res = $orm->get('pb_colg_payment', 't.id', 'DESC', NULL, NULL, $opts)->d;
    echo json_encode($res);
  }
  
  public static function del_payment($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $orm->delete('pb_colg_payment', $req);
    $orm->update('pb_colg_invoice', $req->__obj);
    
    require_once '../include/colg/colgutils.class.php';
    if ($req->enrollment_id != 0) {
      ColgUtils::bal_enrl($orm, $req->enrollment_id);
    }
    ColgUtils::bal_student($orm, $req->student_id);
  }
  
  public static function del_inv($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $orm->delete('pb_colg_invoice', $req);
    
    $opts = new stdClass();
    $opts->cd = 'invoice_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $orm->delete_by_opts('pb_colg_payment', $opts);
    
    require_once '../include/colg/colgutils.class.php';
    if ($req->enrollment_id != 0) {
      ColgUtils::bal_enrl($orm, $req->enrollment_id);
    }
    ColgUtils::bal_student($orm, $req->student_id);
    
    if (isset($req->__pg)) {
      $opts = new stdClass();
      $opts->cd = 't.track_id LIKE :v1 AND t.student_title__ LIKE :v2';
      $opts->cdv = array(':v1' => '%' . $req->__term0 . '%', ':v2' => '%' . $req->__term2 . '%');
      $res = $orm->get('pb_colg_invoice', $req->__o, $req->__q, $req->__pg, SIZE, $opts);
      echo json_encode($res);
    }
  }
}
?>